package com.spring.controller.work.data;

import com.spring.controller.BaseController;
import com.spring.controller.dto.order.Link;
import com.spring.controller.dto.order.People;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * SpringMVC Controller接收参数总结
 * 作者：Heacewalker
 * 链接：https://www.jianshu.com/p/ed44e89a6f79
 * 来源：简书
 * 简书著作权归作者所有，任何形式的转载都请联系作者获得授权并注明出处。
 *
 * @author: chenhs
 * @date: Created in 14:44 2019/8/4
 **/
@Controller
public class ParamController extends BaseController {

    /**
     * 跳转页面
     * @return
     */
    @RequestMapping
    public ModelAndView toParam() {
        return new ModelAndView("/param/toparam");
    }

    /*************************************基本类型*********************************/
    /**基本数据类型
     * SpringMVC Controller各方法参数绑定首先支持Java所有基本类型（包括：  byte、short、int、long、float、double、char、string、boolean），
     * 以及基本类型对应封装高级类（包括：StringBuilder、StringBuffer）。
     *
     */
    @RequestMapping(value="/int", method=RequestMethod.POST)
    @ResponseBody
    public String requestInt(int param) {
        return "Request successful. Post param : Int - " + param;
    }
    @RequestMapping(value="/integer", method= RequestMethod.POST)
    @ResponseBody
    public String requestInteger(Integer param) {
        return "Request successful. Post param : Integer - " + param;
    }
    /**
     * 只能接收 1 - true, 0 - false
     * */
    @RequestMapping(value="/boolean", method= RequestMethod.POST)
    @ResponseBody
    public String requestBoolean(boolean param) {
        return "Request successful. Post param : boolean - " + param;
    }


    /*************************************集合类型*********************************/
    /**
     * 集合类型List
     * 这里拿常用的List和Map举例，如果直接拿集合类作为绑定参数，
     * 写法上与基本类型类似，不同的是需要在参数处增加注解@RequestParam()
     *
     * postman传值：
     * listParam[]  field1
     * listParam[]  field2
     *
     * @param param
     * @return
     */
    @RequestMapping(value="/list", method=RequestMethod.POST)
    @ResponseBody
    public String requestList(@RequestParam("listParam[]") List<String> param) {
        return "Request successful. Post param : List<String> - " + param.toString();
    }

    /**
     * 不支持application/json
     * 集合类型Map
     * Map类型作为参数，只需要使用@RequestParam注解即可，这里Map对应的key-value基本是String类型，
     * value可以为Object
     *
     * postman传值：
     * field1   China
     * field2   America
     * field3   Japan
     *
     * @param param
     * @return
     */
    @RequestMapping(value="/map", method=RequestMethod.POST)
    @ResponseBody
    public String requestList(@RequestParam Map<String, Object> param) {
        return "Request successful. Post param : Map - " + param;
    }

    /**
     * 支持application/json
     *
     * @param param
     * @return
     */
    @RequestMapping(value="/map2", method=RequestMethod.POST)
    @ResponseBody
    public String requestList2(@RequestBody Map<String, Object> param) {
        return "Request successful. Post param : Map - " + param;
    }







    /*************************************自定义类型*********************************/
    /**
     * 自定义类型
     * 主要的不同点就是需要在自定义类前添加@ModelAttribute注解
     *
     * 奇怪，此处参数使用postman的application/json模式接收不到？
     * 只能如下：
     *  name = chenhs
     *  age = 30
     *  relationship["bose"] = BigBoss
     *
     * @param people
     * @return
     */
    @RequestMapping(value="/people", method=RequestMethod.POST)
    @ResponseBody
    public String requestPeople(@ModelAttribute People people) {
        System.out.println(people.toString());
        return "Get request is successful. Post param : User Class - " + people.toString();
    }


    /**
     * postman接收方式如下：
     * address[0].address = AKB
     * address[0].code = 47
     * address[1].address = AKT
     * address[1].code = 48
     *
     * 此处不支持application/json
     *
     * @param link
     * @return
     */
    @RequestMapping(value="/link", method=RequestMethod.POST)
    @ResponseBody
    public String requestLink(@ModelAttribute Link link) {
        return "Get request is successful. Post param : User Class - " + link.toString();
    }

    /*************************************通过URL路径传递参数并接收*********************************/
    @RequestMapping(value="/path/{key}/{value}", method=RequestMethod.POST)
    @ResponseBody
    public String requestPath(@PathVariable String key, @PathVariable String value) {
        return "Get request is successful. Path param : key - " + key + "; value - " + value;
    }

    /*************************************用HttpServletRequest接收请求参数*********************************/
    @RequestMapping(value="/request", method=RequestMethod.POST)
    @ResponseBody
    public String request(HttpServletRequest arg0, HttpServletResponse arg1) {
        return "Get request is successful. Post param : param1 - " + arg0.getParameter("param1") +
                "; param2 - " + arg0.getParameter("param2") +
                "; param3 - " + arg0.getParameter("param3");
    }

}
